package com.shhm.common.utils;

import com.github.benmanes.caffeine.cache.Cache;
import com.shhm.common.entity.admin.SysRoleOperatePerms;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author JackZ
 * @version 1.0
 * @description: TODO
 * @date 2025/9/6 下午8:25
 */
public class LocalCacheUtil {
    public static final String CACHE_PERMS_KEY_PREFIX = "SYSTEM:PERMS:";
    public static final String CACHE_AUTH_KEY_PREFIX = "SYSTEM:AUTH:";

//    public static void

    public static void clearAllLocalCache(Cache localCache){
        localCache.invalidateAll();
    }

    public static void flushOpePermsLocalCache(Cache localCache, List<SysRoleOperatePerms> list){
        localCache.invalidateAll();
        Map<String, List<SysRoleOperatePerms>> roleOpeMap = list.stream().collect(Collectors.groupingBy(SysRoleOperatePerms::getRoleKey));
        roleOpeMap.forEach((k,v)->localCache.put(LocalCacheUtil.CACHE_PERMS_KEY_PREFIX+k,v));
    }

    public static SysRoleOperatePerms getOperatePermsObjectByRole(Cache<String, List<SysRoleOperatePerms>> localCache,List<String> roles, String requestUri){
        SysRoleOperatePerms objRes = null;
        for (String role : roles) {
            List<SysRoleOperatePerms> permsList = localCache.getIfPresent(CACHE_PERMS_KEY_PREFIX + role);
            SysRoleOperatePerms rolePerms = SysPermsUtil.getOperatePermsObjectByURI(permsList, requestUri);
        }
        return objRes;
    }
}
